package com.ruoyi.gp.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import com.ruoyi.gp.service.IOptionsService;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.gp.domain.vo.QuestionVo;
import com.ruoyi.gp.domain.bo.QuestionBo;
import com.ruoyi.gp.service.IQuestionService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 问题
 *
 * @author ruoyi
 * @date 2024-03-08
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/question")
public class QuestionController extends BaseController {

    private final IQuestionService iQuestionService;

    private final IOptionsService iOptionsService;
    /**
     * 查询问题列表
     */
    @SaCheckPermission("system:question:list")
    @GetMapping("/list")
    public TableDataInfo<QuestionVo> list(QuestionBo bo, PageQuery pageQuery) {
        return iQuestionService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出问题列表
     */
    @SaCheckPermission("system:question:export")
    @Log(title = "问题", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(QuestionBo bo, HttpServletResponse response) {
        List<QuestionVo> list = iQuestionService.queryList(bo);
        ExcelUtil.exportExcel(list, "问题", QuestionVo.class, response);
    }

    /**
     * 获取问题详细信息
     *
     * @param questionId 主键
     */
    @SaCheckPermission("system:question:query")
    @GetMapping("/{questionId}")
    public R<QuestionVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long questionId) {
        return R.ok(iQuestionService.queryById(questionId));
    }

    /**
     * 新增问题
     */
    @SaCheckPermission("system:question:add")
    @Log(title = "问题", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody QuestionBo bo) {
        return toAjax(iQuestionService.insertByBo(bo));
    }

    /**
     * 修改问题
     */
    @SaCheckPermission("system:question:edit")
    @Log(title = "问题", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody QuestionBo bo) {
        return toAjax(iQuestionService.updateByBo(bo));
    }

    /**
     * 删除问题
     *
     * @param questionIds 主键串
     */
    @SaCheckPermission("system:question:remove")
    @Log(title = "问题", businessType = BusinessType.DELETE)
    @DeleteMapping("/{questionIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] questionIds) {
        return toAjax(iQuestionService.deleteWithValidByIds(Arrays.asList(questionIds), true));
    }

    @SaCheckPermission("system:question:relevancy")
    @Log(title = "访视量表-问卷")
    @GetMapping("/relevancy/{sid}")
    public R<List<QuestionVo>> queryByQuestion(@NotNull(message = "主键不能为空") @PathVariable Long sid){
        return R.ok(iQuestionService .queryByQuestion(sid));
    }
}
